﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TriQuest_World_Editor
{
    public partial class Form1 : Form
    {
        SGP.ManagedDirect3D D3D = SGP.ManagedDirect3D.Instance;
        SGP.ManagedTextureManager TM = SGP.ManagedTextureManager.Instance;

        Size TileSetSize = new Size(1, 1);
        Size TileSize = new Size(36, 36);
        Size MapSize = new Size(5, 5);

        Point[,] map = new Point[40, 40];
        Point SelectedTile = new Point(0, 0);

        bool Looping = true;

        public bool Looping1
        {
            get { return Looping; }
            set { Looping = value; }
        }

        CTile Tile = new CTile();

        Bitmap TileSet = Properties.Resources._default;

        int one;

        public Form1()
        {
            InitializeComponent();

            Looping = true;

            D3D.InitManagedDirect3D( splitContainer1.Panel2 );
            D3D.InitManagedDirect3D( splitContainer2.Panel2 );

            TM.InitManagedTextureManager( D3D.Device, D3D.Sprite );

            one = TM.LoadTexture( "../../../../TriQuest/resource/Tiles/_default.png", 0 );

            MapSize.Height  = (int)numericUpDownHeight1.Value;
            MapSize.Width   = (int)numericUpDownWidth1.Value;

            TileSize.Height = (int)numericUpDownHeight2.Value;
            TileSize.Width  = (int)numericUpDownWidth2.Value;
        }

        public void Render()
        {
            Rectangle Rect = new Rectangle(0, 0, TileSet.Width, TileSet.Height);

            D3D.ChangeDisplayParam(splitContainer1.Panel2);

            D3D.Clear( splitContainer1.Panel2, Color.LightGray );

            D3D.DeviceBegin();
            D3D.SpriteBegin();

            //////// THIS IS FOR SPLIT CONTAINER 1 - PANEL 2 RENDER ////////

            Point offset = new Point(0, 0);

            offset.X = splitContainer1.Panel2.AutoScrollPosition.X;
            offset.Y = splitContainer1.Panel2.AutoScrollPosition.Y;

            for (int x = 0; x < MapSize.Width; x++)
            {
                for (int y = 0; y < MapSize.Height; y++)
                {
                    Rectangle destRect = Rectangle.Empty;
                    destRect.X = offset.X + (x * TileSize.Width);
                    destRect.Y = offset.Y + (y * TileSize.Height);
                    destRect.Size = TileSize;

                    Rectangle srcRect = Rectangle.Empty;
                    srcRect.X = (map[x, y].X * TileSize.Width);
                    srcRect.Y = (map[x, y].Y * TileSize.Height);
                    srcRect.Size = TileSize;

                    TM.Draw(one, destRect.X, destRect.Y, 1, 1, srcRect, 0, 0, 0, 0);
                }
            }

            D3D.SpriteEnd();

            for (int x = 0; x < MapSize.Width; x++)
            {
                for (int y = 0; y < MapSize.Height; y++)
                {
                    Rectangle destRect = Rectangle.Empty;
                    destRect.X = offset.X + (x * TileSize.Width);
                    destRect.Y = offset.Y + (y * TileSize.Height);
                    destRect.Size = TileSize;

                    D3D.DrawEmptyRect(destRect, Color.Black);
                }
            }

            splitContainer1.Panel2.AutoScrollMinSize = new Size(TileSize.Width * MapSize.Width, TileSize.Height * MapSize.Height);

            //////// THE END OF SPLIT CONTAINER 1  - PANEL 2 RENDER ////////

            D3D.DeviceEnd();

            D3D.Present();

            D3D.ChangeDisplayParam(splitContainer2.Panel2);

            D3D.Clear( splitContainer2.Panel2, Color.LightGray );

            D3D.DeviceBegin();
            D3D.SpriteBegin();

            //////// THIS IS FOR SPLIT CONTAINER 2 - PANEL 2 RENDER ////////

            if( TileSize.Width > 0 && TileSize.Height > 0 )
                TileSetSize = new Size( TileSet.Width / TileSize.Width, TileSet.Height / TileSize.Height );

            offset = new Point(0, 0);

            offset.X = splitContainer2.Panel2.AutoScrollPosition.X;
            offset.Y = splitContainer2.Panel2.AutoScrollPosition.Y;

            Rectangle selRect = new Rectangle( SelectedTile.X * TileSize.Width, SelectedTile.Y * TileSize.Height, TileSize.Width, TileSize.Height );

            TM.Draw(one, offset.X, offset.Y, 1, 1, Rect, 0, 0, 0, 0);

            D3D.SpriteEnd();

            for (int x = 0; x < TileSetSize.Width; x++)
            {
                for (int y = 0; y < TileSetSize.Height; y++)
                {
                    Rectangle destRect = Rectangle.Empty;
                    destRect.X = offset.X + (x * TileSize.Width);
                    destRect.Y = offset.Y + (y * TileSize.Height);
                    destRect.Size = TileSize;

                    D3D.DrawEmptyRect( destRect, Color.Black );
                }
            }

            if (SelectedTile.X * TileSize.Width < TileSetSize.Width * TileSize.Width && SelectedTile.Y * TileSize.Height < TileSetSize.Height * TileSize.Height)
                D3D.DrawEmptyRect( selRect, Color.Red );

            splitContainer2.Panel2.AutoScrollMinSize = new Size(TileSet.Width, TileSet.Height);

            //////// THE END OF SPLIT CONTAINER 2  - PANEL 2 RENDER ////////

            D3D.DeviceEnd();

            D3D.Present();
        }

        private void separateAllToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDownHeight1_ValueChanged(object sender, EventArgs e)
        {
            MapSize.Height    = (int)numericUpDownHeight1.Value;
        }

        private void numericUpDownWidth1_ValueChanged(object sender, EventArgs e)
        {
            MapSize.Width     = (int)numericUpDownWidth1.Value;
        }

        private void numericUpDownHeight2_ValueChanged(object sender, EventArgs e)
        {
            TileSize.Height   = (int)numericUpDownHeight2.Value;
        }

        private void numericUpDownWidth2_ValueChanged(object sender, EventArgs e)
        {
            TileSize.Width    = (int)numericUpDownWidth2.Value;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Looping = false;
        }
    }
}
